package com.zyh.demo;

import com.zyh.utils.JDBCUtil;

import java.sql.*;
import java.util.Scanner;

/**
 * @Author zyh
 * @Date 2024/8/23 9:35
 * @Description
 */

public class JDBCDemo1 {
    public static void main(String[] args) throws SQLException {
        // 用户登录
        // 键盘输入
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = input.nextLine();
        System.out.println("请输入密码：");
        String password = input.nextLine();
        //创建数据库连接
        Connection connection = JDBCUtil.getConnection();
        //定义 sql
        String sql = "select * from user where username= ? and password= ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1,username);
        preparedStatement.setObject(2,password);
        ResultSet rs = preparedStatement.executeQuery();
        if (rs.next()) {

            String loginName = rs.getString("username");
            System.out.println("登陆成功！欢迎"+loginName);
        }
        else {
            System.out.println("登陆失败，用户名或者密码错误！");
        }
        JDBCUtil.close(rs,preparedStatement,connection);
    }
    public static void main1(String[] args) throws SQLException {
        // 用户登录
        // 键盘输入
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = input.nextLine();
        System.out.println("请输入密码：");
        String password = input.nextLine();
        //创建数据库连接
        Connection connection = JDBCUtil.getConnection();
        Statement statement = connection.createStatement();
        //定义 sql
        String sql = "select * from user where username='"+username+"' and password='"+password+"'";
        ResultSet rs = statement.executeQuery(sql);
        if (rs.next()) {

            String loginName = rs.getString("username");
            System.out.println("登陆成功！欢迎"+loginName);
        }
        else {
            System.out.println("登陆失败，用户名或者密码错误！");
        }
        JDBCUtil.close(rs,statement,connection);
    }
}
